//
//  BTEBaseTabBarController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEBaseTabBarController: UITabBarController,UITabBarControllerDelegate {
    
    let basetabbar =  BTEBaseTabBarView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
  
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.shadowColor = .white
        tabBar.standardAppearance = tabBarAppearance
        setValue(basetabbar, forKey: "tabBar")
        delegate = self
        
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        guard let basenavigationconrollter = viewController as? BTEBaseNavigationViewController,let root = basenavigationconrollter.children.first else {
            return false
        }
        
        let lo = UserDefaults.standard.value(forKey: USER_TOKEN) as? String
        if ( (root as? BTEOrderViewController != nil ) || (root as? BTEMeViewController != nil)) && (lo ?? "").count <= 0 {
            let tab = BTEBaseNavigationViewController.init(rootViewController: BTELoginViewController())
            tab.modalPresentationStyle = .overFullScreen
            viewController.present(tab, animated: true)
            return false
        }
        return true
    }
    
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let index = tabBarController.selectedIndex
        if index == 0 {
            basetabbar.la.image = UIImage.init(named: "Framefwefwefwefwee")
            basetabbar.ed.image = UIImage.init(named: "fwef32323232er23")
        } else if index == 2 {
            basetabbar.la.image = UIImage.init(named: "gwerger432323232")
            basetabbar.ed.image = UIImage.init(named: "fewfb3r1232323232323")
        }
        
        if index == 1 {
            basetabbar.la.image = UIImage.init(named: "gwerger432323232")
            basetabbar.ed.image = UIImage.init(named: "fwef32323232er23")
        }
    }
    
}



class BTEBaseTabBarView: UITabBar {
    
    let bgview = UIView()
    
    let la = UIImageView.init(image: UIImage.init(named: "Framefwefwefwefwee"))
    
    let centerv = UIImageView.init(image: UIImage.init(named: "Group 1171276885"))
    
    let ed = UIImageView.init(image: UIImage.init(named: "fwef32323232er23"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bgview.backgroundColor = .black
        addSubview(bgview)
        bgview.roundCorners(.allCorners, radius: 63/2)
        bgview.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.height.equalTo(63)
            
            if UIDevice.hasBottomSafeArea() {
                make.bottom.equalToSuperview().offset(-32)
            } else {
                make.bottom.equalToSuperview().offset(-5)
            }
           
        }
        
        addSubview(centerv)
        centerv.isUserInteractionEnabled = false
        centerv.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(-22)
        }
        
        addSubview(la)
        la.isUserInteractionEnabled = false
        la.snp.makeConstraints { make in
            make.centerY.equalTo(bgview)
            let f = (UIScreen.main.bounds.width - 32)/3
            make.left.equalToSuperview().offset(16+(f/2))
        }
        
        addSubview(ed)
        ed.isUserInteractionEnabled = false
        ed.snp.makeConstraints { make in
            make.centerY.equalTo(bgview)
            let f = (UIScreen.main.bounds.width - 32)/3
            make.right.equalToSuperview().offset(-(16+(f/2)))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}



extension UIDevice {
   static func hasBottomSafeArea() -> Bool {
        let keyWindow = UIApplication.shared.connectedScenes
            .compactMap { $0 as? UIWindowScene }
            .flatMap { $0.windows }
            .first { $0.isKeyWindow }

        let bottomInset = keyWindow?.safeAreaInsets.bottom ?? 0
        return bottomInset > 0
    }
}
